package com.jftone.bh.app.action;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.StaticInfoQry;
import com.jftone.bh.app.model.SysArea;
import com.jftone.bh.app.service.AreaService;
import com.jftone.bh.app.service.RecordService;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.exception.ActionException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.Page;
import org.jftone.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller(mapping="/recordAction")
public class RecordAction extends BaseAction {

	@Autowired
	private RecordService service;

	private Logger logger = Logger.getLogger(RecordAction.class);

	/**
	 * 获取流水记录
	 * @throws ActionException
	 */
	public void query() throws ActionException {
        IData<String, Object> data = getData();
        String account = data.getString("account", "");
        String cfNo = data.getString("cfNo", "");
        String status = data.getString("status", "");
        String userId = data.getString("userId", "");
        String sTime = data.getString("sTime", "");
        String eTime = data.getString("eTime", "");
        String currency = data.getString("currency", "");
        String type = data.getString("type", "");
        String mtype = data.getString("mtype", "");
        this.putRenderInfo("currency", currency);
        this.putRenderInfo("type", type);
        this.putRenderInfo("mtype", mtype);
        this.putRenderInfo("sTime", sTime);
        this.putRenderInfo("eTime", eTime);
        this.putRenderInfo("userId", userId);
        this.putRenderInfo("status", status);
        this.putRenderInfo("cfNo", cfNo);
        this.putRenderInfo("account", account);
        try {
            //设置功能按钮
            setMenuFuncDerictive();
            //设置分页组件
            setPageDerictive();
            putParam("sTime", sTime);
            putParam("eTime", eTime);
            putParam("userId", userId);
            putParam("account", account);
            putParam("currency", currency);
            putParam("type", type);
            putParam("mtype", mtype);
            putParam("cfNo", cfNo);
            putParam("status", status);
            putParam(SysConst.QUERY_PARAMS, getParam());

            this.putRenderInfo("currencyList", StaticInfoQry.getParams(SysConst.RECORD_CURRENCY));
            this.putRenderInfo("typeList", StaticInfoQry.getParams(SysConst.RECORD_TYPE));
            this.putRenderInfo("mtypeList", StaticInfoQry.getParams(SysConst.RECORD_MTYPE));
            this.putRenderInfo("statusList", StaticInfoQry.getParams(SysConst.RECORD_STATUS));
            List<IData<String, Object>> list = service.queryList(data, getPage());
            this.putRenderInfo("list", list);
            this.render("record/record_list.html");
        } catch (Exception e) {
            logger.error("查询流水记录列表错误" + e.getMessage(), e);
            throw new ActionException("查询流水记录列表错误" + e.getMessage(), e);
        }
	}
    /**
     * 获取提现流水记录
     * @throws ActionException
     */
    public void queryWithdrawList() throws ActionException {
        IData<String, Object> data = getData();
        String account = data.getString("account", "");
        String cfNo = data.getString("cfNo", "");
        String status = data.getString("status", "");
        String userId = data.getString("userId", "");
        String sTime = data.getString("sTime", "");
        String eTime = data.getString("eTime", "");
        this.putRenderInfo("sTime", sTime);
        this.putRenderInfo("eTime", eTime);
        this.putRenderInfo("userId", userId);
        this.putRenderInfo("status", status);
        this.putRenderInfo("cfNo", cfNo);
        this.putRenderInfo("account", account);
        try {
            //设置功能按钮
            setMenuFuncDerictive();
            //设置分页组件
            setPageDerictive();
            putParam("sTime", sTime);
            putParam("eTime", eTime);
            putParam("userId", userId);
            putParam("account", account);
            putParam("cfNo", cfNo);
            putParam("status", status);
            putParam(SysConst.QUERY_PARAMS, getParam());

            this.putRenderInfo("statusList", StaticInfoQry.getParams(SysConst.RECORD_STATUS));
            List<IData<String, Object>> list = service.queryWithdrawList(data, getPage());
            this.putRenderInfo("list", list);
            this.render("withdraw/withdraw_list.html");
        } catch (Exception e) {
            logger.error("查询提现流水记录列表错误" + e.getMessage(), e);
            throw new ActionException("查询提现流水记录列表错误" + e.getMessage(), e);
        }
    }

    /**
     * 获取提现流水记录
     * @throws ActionException
     */
    public void audit() throws ActionException {
        IData<String, Object> data = getData();
        String dataId = data.getString("dataId");
        if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
            this.alertMsg("参数传值错误");
            return;
        }
        try {
            IData<String, Object> record = service.getWithdrawData(Long.parseLong(dataId));
            this.putRenderInfo("statusList", StaticInfoQry.getParams(SysConst.RECORD_STATUS));
            this.putRenderInfo("record", record);
            this.putRenderInfo("auditStatus", record.getShort("AUDIT_STATUS"));
            this.putRenderInfo("stateList", (List<IData<String, Object>>)record.get("stateList"));
            record.remove("stateList");
            this.render("withdraw/withdraw_audit.html");
        } catch (Exception e) {
            logger.error("加载提现流水错误"+e.getMessage(), e);
            throw new ActionException("提现流水记录错误"+e.getMessage(), e);
        }
    }

    /**
     * 审核提现
     * @throws ActionException
     */
    public void saveWithdrawAudit() throws ActionException {
        IData<String, Object> data = getData();
        String recordId = data.getString("recordId");
        String newAuditStatus = data.getString("newAuditStatus");
        String remark = data.getString("remark");
        if(StringUtil.isBlank(remark) ||StringUtil.isBlank(recordId) || !StringUtil.isNumber(recordId)
                || StringUtil.isBlank(newAuditStatus) || !StringUtil.isNumber(newAuditStatus)){
            this.alertMsg("参数传值错误");
            return;
        }
        try {
            HttpSession session = this.getRequest().getSession(false);
            IData<String, Object> admin = (IData<String, Object>)session.getAttribute(SysConst.ADMIN_USER);
            IData<String, Object> retData = service.saveWithdrawAudit(Long.parseLong(recordId),Short.parseShort(newAuditStatus),remark,admin.getString("adminUser"));
            if(retData.getInt("resultCode",1) == 0){
                this.showMsg("操作成功", "?method=queryWithdrawList");
                return;
            }else{
                this.errorMsg("操作失败","提现审核失败",retData.getString("resultMsg"), "?method=queryWithdrawList");
                return;
            }
        } catch (Exception e) {
            logger.error("审核提现错误"+e.getMessage(), e);
            throw new ActionException("审核提现错误"+e.getMessage(), e);
        }
    }


}
